home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Utilities / StorUtil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  13.3 KB  |  484 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        StorUtil.cpp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Owned by:    Craig Carper
  7.  
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <6>    20.09.1996    NP        1386078: GetXXXContainer should be
  13.                                     AcquireXXXContainer in StorUtil
  14.          <5>      9/4/96    EL        1343245: move BestContentType from
  15.                                     LnkDlgEx.cpp to here.
  16.          <4>     6/24/96    RA        1355839: SOM_TRY => TRY
  17.          <3>     3/26/96    CC        1333083: RemoveDataInterchangeProperties
  18.                                     removes kODPropOriginalID property.
  19.          <2>     3/15/96    CC        1316917: Changed
  20.                                     RemoveDataInterchangeProperties parameter
  21.                                     name to "keepProxyProperties".
  22.  
  23.     To Do:
  24. */
  25.  
  26. /*
  27.     File:        StorUtil.cpp
  28.  
  29.     Contains:    Utilities for working with ODStorageUnits.
  30.  
  31.     Owned by:    Vincent Lo
  32.  
  33.     Copyright:    © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  34.  
  35.     
  36.     In Progress:
  37.         
  38. */
  39.  
  40. #ifndef _STORUTIL_
  41. #include <StorUtil.h>
  42. #endif
  43.  
  44. #ifndef SOM_ODDraft_xh
  45. #include <Draft.xh>
  46. #endif
  47.  
  48. #ifndef SOM_ODSession_xh
  49. #include <ODSessn.xh>
  50. #endif
  51.  
  52. #ifndef SOM_ODStorageSystem_xh
  53. #include <ODStor.xh>
  54. #endif
  55.  
  56. #ifndef SOM_ODDocument_xh
  57. #include <Document.xh>
  58. #endif
  59.  
  60. #ifndef SOM_ODContainer_xh
  61. #include <ODCtr.xh>
  62. #endif
  63.  
  64. #ifndef SOM_ODStorageUnit_xh
  65. #include <StorageU.xh>
  66. #endif
  67.  
  68. #ifndef SOM_ODStorageUnitView_xh
  69. #include <SUView.xh>
  70. #endif
  71.  
  72. #ifndef SOM_Module_OpenDoc_StdDefs_defined
  73. #include <StdDefs.xh>
  74. #endif
  75.  
  76. #ifndef SOM_Module_OpenDoc_StdProps_defined
  77. #include <StdProps.xh>
  78. #endif
  79.  
  80. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  81. #include "StdTypes.xh"
  82. #endif
  83.  
  84. #ifndef SOM_ODTranslation_xh
  85. #include <Translt.xh>
  86. #endif
  87.  
  88. #ifndef _BARRAY_
  89. #include <BArray.h>
  90. #endif
  91.  
  92. #ifndef _STDTYPIO_
  93. #include <StdTypIO.h>
  94. #endif
  95.  
  96. #ifndef _PLFMFILE_
  97. #include <PlfmFile.h>
  98. #endif
  99.  
  100. #ifndef _EXCEPT_
  101. #include <Except.h>
  102. #endif
  103.  
  104. #ifndef _ODMEMORY_
  105. #include <ODMemory.h>
  106. #endif
  107.  
  108. #ifndef __TEXTEDIT__
  109. #include <TextEdit.h>
  110. #endif
  111.  
  112. #ifndef _TEMPOBJ_
  113. #include <TempObj.h>
  114. #endif
  115.  
  116. //==============================================================================
  117. // ODSUAddPropValue
  118. //==============================================================================
  119.  
  120. void        ODSUAddPropValue(Environment* ev,
  121.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  122. {
  123.     su->AddProperty(ev, prop)->AddValue(ev, val);
  124. }
  125.  
  126. //==============================================================================
  127. // ODSUForceFocus
  128. //==============================================================================
  129.  
  130. void        ODSUForceFocus(Environment* ev, 
  131.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  132. {
  133.     if (prop != kODNULL) {
  134.         if (su->Exists(ev, prop, kODNULL, 0) == kODFalse)
  135.             su->AddProperty(ev, prop);
  136.         else
  137.             su->Focus(ev, prop, kODPosUndefined, kODNULL, 0, kODPosUndefined);
  138.     }
  139.     if (val != kODNULL) { 
  140.         if (su->Exists(ev, prop, val, 0) == kODFalse)
  141.             su->AddValue(ev, val);
  142.         else
  143.             su->Focus(ev, prop, kODPosSame, val, 0, kODPosUndefined);
  144.     }
  145.     // else; // Presumably the caller has already focussed the su somewhere,
  146.              // perhaps midstream. -TC
  147. }
  148.  
  149. //==============================================================================
  150. // ODSUExistsThenFocus
  151. //==============================================================================
  152.  
  153. ODBoolean        ODSUExistsThenFocus(Environment* ev, 
  154.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  155. {
  156.     if (prop == kODNULL && val == kODNULL)
  157.         return kODTrue;
  158.     // else; // Presumably the caller has already focussed the su somewhere,
  159.              // perhaps midstream. Property:kODNULL && ValueType:kODNULL always 'exist'. -TC
  160.              
  161.     if (su->Exists(ev, prop, val, 0))
  162.     {
  163.         if ( val )
  164.             su->Focus(ev,  prop, kODPosSame, val, 0, kODPosSame);
  165.         else
  166.             su->Focus(ev,  prop, kODPosSame, kODNULL, 0, kODPosAll);
  167.         return kODTrue;    
  168.     }
  169.     else
  170.     {
  171.         return kODFalse;
  172.     }
  173. }
  174.  
  175. //==============================================================================
  176. // ODSURemoveProperty
  177. //==============================================================================
  178.  
  179. void ODSURemoveProperty(Environment* ev, ODStorageUnit* su, ODPropertyName prop)
  180. {
  181.     if ( ODSUExistsThenFocus(ev, su, prop, kODNULL) )
  182.         su->Remove(ev);
  183. }
  184.  
  185. //----------------------------------------------------------------------------------------
  186. // GetPlatformFileFromContainer
  187. //----------------------------------------------------------------------------------------
  188. PlatformFile*    GetPlatformFileFromContainer(Environment* ev, ODContainer* container)
  189. {
  190.     ODByteArray    ba = container->GetID(ev);
  191.     PlatformFile*    file = new PlatformFile();
  192.     file->Specify((ODFileSpec*) ba._buffer);
  193.     ODDisposePtr(ba._buffer);
  194.     
  195.     return file;
  196. }
  197.  
  198. //----------------------------------------------------------------------------------------
  199. // GetODFileSpecFromContainer
  200. //----------------------------------------------------------------------------------------
  201. ODFileSpec GetODFileSpecFromContainer(Environment* ev, ODContainer* container)
  202. {
  203.     ODByteArray    ba = container->GetID(ev);
  204.     ODFileSpec documentSpec = *((ODFileSpec*) ba._buffer);
  205.     ODDisposePtr(ba._buffer); // DMc dispose when done
  206.  
  207.     return documentSpec;
  208. }
  209.  
  210. //----------------------------------------------------------------------------------------
  211. // CreateFileContainer
  212. //----------------------------------------------------------------------------------------
  213. ODContainer*    CreateFileContainer(Environment* ev, ODSession* session, ODFileSpec* fsSpec)
  214. {
  215.     ODByteArray*    ba = CreateByteArray(fsSpec, sizeof(short) + sizeof(long) + fsSpec->name[0] + 1);
  216.     ODContainer*    newContainer = session->GetStorageSystem(ev)->
  217.         CreateContainer(ev,kODDefaultFileContainer /*file->GetContainerType()*/,ba);
  218.     DisposeByteArray(ba);
  219.     
  220.     return newContainer;
  221. }
  222.  
  223. //----------------------------------------------------------------------------------------
  224. // AcquireFileContainer
  225. //----------------------------------------------------------------------------------------
  226. ODContainer*    AcquireFileContainer(Environment* ev, ODSession* session, ODFileSpec* fsSpec)
  227. {
  228.     ODByteArray*    ba = CreateByteArray(fsSpec, sizeof(short) + sizeof(long) + fsSpec->name[0] + 1);
  229.     ODContainer*    newContainer = session->GetStorageSystem(ev)->
  230.         AcquireContainer(ev,kODDefaultFileContainer /*file->GetContainerType()*/,ba);
  231.     DisposeByteArray(ba);
  232.     
  233.     return newContainer;
  234. }
  235.     
  236. //----------------------------------------------------------------------------------------
  237. // CreateMemoryContainer
  238. //----------------------------------------------------------------------------------------
  239. ODContainer* CreateMemoryContainer(Environment* ev,
  240.                 ODSession* session,
  241.                 ODHandle handle,
  242.                 ODContainerType containerType)
  243. {
  244.     ODByteArray* ba;
  245.     TRY
  246.         ODLockHandle(handle);
  247.         ba = CreateByteArray(&handle, sizeof(ODHandle));
  248.         ODUnlockHandle(handle);
  249.     CATCH_ALL
  250.         ODUnlockHandle(handle);
  251.         RERAISE;
  252.     ENDTRY
  253.  
  254.     ODContainer* newContainer = session->GetStorageSystem(ev)->
  255.         CreateContainer(ev, containerType, ba);
  256.     DisposeByteArray(ba);
  257.     
  258.     return newContainer;
  259. }
  260.  
  261. //----------------------------------------------------------------------------------------
  262. // AcquireMemoryContainer
  263. //----------------------------------------------------------------------------------------
  264. ODContainer* AcquireMemoryContainer(Environment* ev, 
  265.                 ODSession* session,
  266.                 ODHandle handle,
  267.                 ODContainerType containerType)
  268. {
  269.     ODByteArray* ba;
  270.     TRY
  271.         ODLockHandle(handle);
  272.         ba = CreateByteArray(&handle, sizeof(ODHandle));
  273.         ODUnlockHandle(handle);
  274.     CATCH_ALL
  275.         ODUnlockHandle(handle);
  276.         RERAISE;
  277.     ENDTRY
  278.  
  279.     ODContainer* newContainer = session->GetStorageSystem(ev)->
  280.         AcquireContainer(ev, containerType, ba);
  281.     DisposeByteArray(ba);
  282.     
  283.     return newContainer;
  284. }
  285.  
  286. //----------------------------------------------------------------------------------------
  287. // StorageUnitGetValue
  288. //----------------------------------------------------------------------------------------
  289. ODULong    StorageUnitGetValue(ODStorageUnit* su,
  290.                             Environment* ev,
  291.                             ODULong    size,
  292.                             ODPtr buffer)
  293. {
  294.     ODByteArray    ba;    
  295.     ODULong bytesRead = su->GetValue(ev, size, &ba);
  296.     ODBlockMove(ba._buffer, buffer, bytesRead);
  297.     ODDisposePtr(ba._buffer);
  298.  
  299.     return bytesRead;
  300. }
  301.  
  302. //----------------------------------------------------------------------------------------
  303. // StorageUnitViewGetValue
  304. //----------------------------------------------------------------------------------------
  305. ODULong    StorageUnitViewGetValue(ODStorageUnitView* suv,
  306.                             Environment* ev,
  307.                             ODULong    size,
  308.                             ODPtr buffer)
  309. {
  310.     ODByteArray    ba;
  311.     
  312.     ODULong bytesRead = suv->GetValue(ev, size, &ba);
  313.     ODBlockMove(ba._buffer, buffer, ba._length);
  314.     ODDisposePtr(ba._buffer);
  315.     
  316.     return bytesRead;
  317. }
  318.  
  319.  
  320. //----------------------------------------------------------------------------------------
  321. // StorageUnitSetValue
  322. //----------------------------------------------------------------------------------------
  323. void    StorageUnitSetValue(ODStorageUnit* su,
  324.                             Environment* ev,
  325.                             ODULong    size,
  326.                             const void *buffer)
  327. {
  328.     ODByteArray ba;
  329.     ba._length = size;
  330.     ba._maximum = size;
  331.     ba._buffer = (octet*) buffer;
  332.     su->SetValue(ev, &ba);
  333. }
  334.  
  335. //----------------------------------------------------------------------------------------
  336. // StorageUnitViewSetValue
  337. //----------------------------------------------------------------------------------------
  338. void    StorageUnitViewSetValue(ODStorageUnitView* suv,
  339.                                 Environment* ev,
  340.                                 ODULong    size,
  341.                                 const void *buffer)
  342. {
  343.     ODByteArray ba;
  344.     ba._length = size;
  345.     ba._maximum = size;
  346.     ba._buffer = (octet*) buffer;
  347.     suv->SetValue(ev, &ba);
  348. }
  349.  
  350. //----------------------------------------------------------------------------------------
  351. // StorageUnitSetPromiseValue
  352. //----------------------------------------------------------------------------------------
  353. void    StorageUnitSetPromiseValue(ODStorageUnit* su,
  354.                                     Environment* ev,
  355.                                     ODValueType valueType,
  356.                                     ODULong offset,
  357.                                     ODULong    size,
  358.                                     const void *buffer,
  359.                                     ODPart *sourcePart)
  360. {
  361.     ODByteArray ba;
  362.     ba._length = size;
  363.     ba._maximum = size;
  364.     ba._buffer = (octet*) buffer;
  365.     su->SetPromiseValue(ev, valueType, offset, &ba, sourcePart);
  366. }
  367.  
  368. //----------------------------------------------------------------------------------------
  369. // StorageUnitGetStylFromStyledText
  370. //----------------------------------------------------------------------------------------
  371. ODBoolean StorageUnitGetStylFromStyledText(ODStorageUnit* su,
  372.                             Environment* ev,
  373.                             ODULong* size,
  374.                             ODPtr* styl)
  375. {
  376.     ODBoolean    result = kODFalse;
  377.     ODULong        stxtSize;
  378.     ODULong        stylSize;
  379.     ODUShort    scrpNStyles;
  380.     ODType        applestxt = kODNULL;
  381.  
  382.     const ODPlatformType kODScrapType_stxt = 0x73747874;    // 'stxt'
  383.  
  384.     ODVolatile(applestxt);
  385.     ODVolatile(result);
  386.     
  387.     TRY
  388.  
  389.         ODTranslation* translation = su->GetSession(ev)->GetTranslation(ev);
  390.         applestxt = translation->GetISOTypeFromPlatformType(ev, kODScrapType_stxt, kODPlatformDataType);
  391.             
  392.         if (ODSUExistsThenFocus(ev, su, kODPropContents, applestxt) )
  393.         {
  394.             stxtSize = su->GetSize(ev);
  395.             if ( stxtSize >= sizeof(ODUShort) )
  396.             {
  397.                 StorageUnitGetValue(su, ev, sizeof(ODUShort), &scrpNStyles);
  398.                 stylSize = (scrpNStyles * sizeof(ScrpSTElement)) + sizeof(ODUShort);
  399.                 if ( stxtSize >= stylSize )
  400.                 {
  401.                     *styl = ODNewPtr(stylSize);
  402.                     *size = stylSize;
  403.                     su->SetOffset(ev, 0);
  404.                     StorageUnitGetValue(su, ev, stylSize, *styl);
  405.                     result = kODTrue;
  406.                 }
  407.             }
  408.         }
  409.         
  410.         delete applestxt;
  411.     
  412.     CATCH_ALL
  413.             if ( applestxt )
  414.                 delete applestxt;
  415.             result = kODFalse;
  416.     ENDTRY
  417.  
  418.     return result;
  419. }
  420.  
  421. //------------------------------------------------------------------------------
  422. // GetOriginalCloneKind
  423. //------------------------------------------------------------------------------
  424.  
  425. ODCloneKind GetOriginalCloneKind(Environment* ev, ODDraft* draft)
  426. {
  427.     // If content was put on the clipboard without cloning, there will be no
  428.     // kODPropOriginalCloneKind property in the draft's preferences storage
  429.     // unit.  Assume the original operation was a copy in this case. Since
  430.     // no links cannot be placed on the clipboard directly, there won't be
  431.     // links needing fixup on a paste.
  432.  
  433.     TempODStorageUnit draftProperties = draft->AcquireDraftProperties(ev);
  434.     ODCloneKind cloneKind = ODGetULongProp(ev, draftProperties, kODPropOriginalCloneKind, kODULong);
  435.     
  436.     if ((ODULong)(cloneKind) != 0)
  437.         return cloneKind;
  438.     else
  439.         return kODCloneCopy;
  440. }
  441.  
  442. //------------------------------------------------------------------------------
  443. // RemoveDataInterchangeProperties
  444. //------------------------------------------------------------------------------
  445.  
  446. void RemoveDataInterchangeProperties (Environment* ev,
  447.         ODStorageUnit*    su,
  448.         ODBoolean        keepProxyProperties)
  449. {
  450.     ODSURemoveProperty(ev, su, kODPropLinkSpec);
  451.     ODSURemoveProperty(ev, su, kODPropMouseDownOffset);
  452.     ODSURemoveProperty(ev, su, kODPropCloneKindUsed);
  453.     ODSURemoveProperty(ev, su, kODPropOriginalID);
  454.     if ( !keepProxyProperties )
  455.     {
  456.         ODSURemoveProperty(ev, su, kODPropProxyContents);
  457.         ODSURemoveProperty(ev, su, kODPropContentFrame);
  458.         ODSURemoveProperty(ev, su, kODPropSuggestedFrameShape);
  459.     }
  460. }
  461.  
  462. #ifdef _OD_IMPL_
  463. //------------------------------------------------------------------------------
  464. // BestContentKind
  465. //------------------------------------------------------------------------------
  466. ODType BestContentKind(ODStorageUnit* contentSU)
  467. {
  468.     Environment* ev = somGetGlobalEnvironment();
  469.  
  470.     ODType bestKind = ODGetISOStrProp(ev, contentSU, kODPropPreferredKind, kODISOStr, kODNULL, kODNULL);
  471.  
  472.     if ( bestKind == kODNULL )
  473.     {
  474.         if (contentSU->Exists(ev, kODPropContents, (ODValueType)kODNULL, 1))
  475.         {
  476.             contentSU->Focus(ev, kODPropContents, kODPosUndefined, (ODValueType)kODNULL, 1, kODPosUndefined);
  477.             bestKind = contentSU->GetType(ev);
  478.         }
  479.     }
  480.     
  481.     return bestKind;
  482. }
  483. #endif
  484.